
import requests
from lxml import etree
import csv

#登录并获取动态token
url="http://boweifeng.xueqingyun.com/user/sign-in/login"
# response=requests.get(url).text
Session=requests.session()
response=Session.get(url).text
# print(type(response))
#转化为dom格式
dom=etree.HTML(response)
# print(type(dom))
#xpath定位concent内容
#后面加[0]，只取到里面的值
token=dom.xpath('//meta[@name="csrf-token"]/@content')[0]
print(token)

userinfo={
    "_csrf":token,
    "LoginForm[identity]":"xzmadmin",
    "LoginForm[password]":"51testing",
    "LoginForm[rememberMe]":"0",
    "login-button":""
}
response2=Session.post(url,data=userinfo).text
# print(response2)

file=open("testdata.csv", "r")
rows=csv.reader(file)
for row in rows:
    # print(row[1])
    # print(row[2])
#进入添加学生页面
# testurl="http://boweifeng.xueqingyun.com/schadmin/user/add-stu"
# testurl="http://boweifeng.xueqingyun.com/schadmin/subject/create"
    testurl=row[1]
    response3=Session.get(testurl).text
# print(response3)
    dom2=etree.HTML(response3)
# print(dom2)
#从form标签开始定位,下面所有的label标签，然后使用text()属性获取文本信息
# result=dom2.xpath('//form//label/text()')
    result=dom2.xpath('//form//'+str(row[2])+'/text()')
    print(result)
